package com.zhongyi.util;

import java.net.URL;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;

/**
 * 邮件发送辅助工具类
 * <p>
 * 
 *
 * @author <a href="mailto:lysongfei@gmail.com">songfei</a>
 * @version 3.0, 2012-4-24
 */
public class MailUtil {
	
	private MailConfiguration mailConfig;
	
	public void setMailConfig(MailConfiguration mailConfig) {
		this.mailConfig = mailConfig;
	}
	
	public static boolean sendMail(String mailTo,String mailSubject,String message,String templatePath,List<String> attachList){
		boolean status = false;
		
		return status;
	}
	
	public boolean sendTextMail(String mailTo,String mailSubject,String message){
		boolean status = false;
		SimpleEmail  email=new SimpleEmail ();
		email.setHostName(mailConfig.getSmtp());
		email.setCharset(mailConfig.getCharset());
		email.setAuthentication(mailConfig.getUser(), mailConfig.getPwd());
		try {
			email.addTo(mailTo);
			email.setFrom(mailConfig.getUser());
			email.setSubject(mailSubject);
			email.setMsg(message);
			email.send();
			status = true;
		} catch (EmailException e) {
			logger.error("error send text mail to " + mailTo + " ..." , e);
		}
		return status;
	}
	
	public boolean sendHtmlMail(String mailTo,String mailSubject,String message,String htmlTemplatePath){
		boolean status = false;
		HtmlEmail email = new HtmlEmail();
		email.setHostName(mailConfig.getSmtp());
		email.setCharset(mailConfig.getCharset());
		email.setAuthentication(mailConfig.getUser(), mailConfig.getPwd());
		try {
//			EmailAttachment attachment = new EmailAttachment();
//			attachment.setURL(new URL("http://www.zhongyifoundry.com/wp-content/themes/twentyeleven/images/zhongyi_new.jpg"));
//			attachment.setDisposition(EmailAttachment.ATTACHMENT);
//			attachment.setDescription("Company Logo");
//			attachment.setName("zhongyifoundry");
			
//			String html = StringUtil.readHtmlToString(htmlTemplatePath);
			email.addTo(mailTo);
			email.setFrom(mailConfig.getUser());
			email.setSubject(mailSubject);
			email.setHtmlMsg(htmlTemplatePath);
			email.setTextMsg(message);
//			email.attach(attachment);
			email.send();
			status = true;
			logger.info("success send mail to " + mailTo);
		} catch (Exception e) {
			logger.error("error send html mail to " + mailTo + " ..." , e);
		}
		return status;
	}
	
	public boolean sendHtmlMailWithImg(String mailTo,String mailObject,String message,String imgPath){
		boolean status = false;
		HtmlEmail email = new HtmlEmail();
		email.setHostName(mailConfig.getSmtp());
		email.setCharset(mailConfig.getCharset());
		email.setAuthentication(mailConfig.getUser(), mailConfig.getPwd());
		try {
			email.addTo(mailTo);
			email.setFrom(mailConfig.getUser());
			email.setSubject(mailObject);
			URL url = new URL(imgPath);
			String cid = email.embed(url, "zhongyi logo");
			
			email.setHtmlMsg("<html>The zhongyifoundry logo - <img src=\"cid:"+cid+"\"></html>");
			email.send();
			status = true;
		} catch (Exception e) {
			logger.error("error send html mail to " + mailTo + " ...", e);
		}
		return status;
	}
	
	public boolean sendMailWithAttachment(String mailTo,String mailObject,String message,List<String> attPathList){
		boolean status = false;
		HtmlEmail email = new HtmlEmail();
		email.setHostName(mailConfig.getSmtp());
		email.setCharset("GB2312");
		email.setAuthentication(mailConfig.getUser(), mailConfig.getPwd());
		EmailAttachment attachment = new EmailAttachment(); 
		attachment.setPath("c:\\Sql Server导出数据至Oracle方法.doc");
		attachment.setDisposition(EmailAttachment.ATTACHMENT); 
		attachment.setName("Sql Server导出数据至Oracle方法.doc");
		String html = StringUtil.readHtmlToString("c:\\test.html");
		try {
			email.addTo("lysongfei@126.com");
			email.setFrom("info@zhongyifoundry.com");
			email.setSubject("common mail 附件发送测试 ");
			email.setHtmlMsg(html);
			email.attach(attachment);
			email.send();
			status = true;
		} catch (Exception e) {
			logger.error("error send html mail to " + mailTo + " ...", e);
		}
		return status;
	}
	
	private static Logger logger = Logger.getLogger(MailUtil.class);
}
